package zhieasy.cn.xpu_helper.controller.book_market;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.book_market.BookCommentRely;
import zhieasy.cn.model.book_market.BookCommentRoot;
import zhieasy.cn.service.book_market.BookCommentRelyService;
import zhieasy.cn.service.book_market.BookCommentRootService;

import java.util.List;


/**
 * @author wang zeya
 * @date 2020/8/30 16:53
 */
@RestController
@RequestMapping("/book-market/common")
@Api(tags = "二手书城，评论相关")
public class BookCommentController {

    @Autowired
    private BookCommentRootService bookCommentRootService;

    @Autowired
    private BookCommentRelyService bookCommentRelyService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiOperation(value = "给帖子增加根评论", notes = "就是一级评论，评论的评论叫做二级评论")
    public ResultVO addRootCommon(@ApiParam("失物招领评论类") @RequestBody BookCommentRoot bookCommentRoot) {
        bookCommentRoot.setCreateTime(System.currentTimeMillis());
        bookCommentRoot.setValid(1);
        bookCommentRoot.setUpdateTime(System.currentTimeMillis());
        bookCommentRoot.setType(1);
        bookCommentRoot.setLikeNum(0L);
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        UserDetails userDetails = (UserDetails)auth.getPrincipal();
        boolean idSave = bookCommentRootService.save(bookCommentRoot);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }


    @RequestMapping(value = "/{rootCommonId}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除根评论", notes = "删除根评论，也就是一级评论")
    public ResultVO deleteRootCommon(@ApiParam("根评论的id") @PathVariable("rootCommonId") long rootCommonId) {
        boolean b = bookCommentRootService.removeById(rootCommonId);
        if (b) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @RequestMapping(value = "/{bookId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取评论", notes = "获取某个帖子的评论")
    public ResultVO getRootCommonByBookId(@ApiParam("要获取评论的帖子的id") @PathVariable("bookId") String bookId,
                                           @ApiParam("页数") @RequestParam("pageIndex") int pageIndex,
                                           @ApiParam("每页要多少数据") @RequestParam("pageSize") int pageSize) {

        List<BookCommentRoot> bookCommentRootList = bookCommentRootService.getCommonRootByBookId(bookId, pageIndex, pageSize);
        return ResultUtils.success(bookCommentRootList);
    }


    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ApiOperation(value = "增加二级评论", notes = "评论的评论叫做二级评论")
    public ResultVO addCommonToRoot(@ApiParam("失物招领评论类") @RequestBody BookCommentRely bookCommentRely) {
        bookCommentRely.setCreateTime(System.currentTimeMillis());
        bookCommentRely.setValid(1);
        bookCommentRely.setUpdateTime(System.currentTimeMillis());
        boolean idSave = bookCommentRelyService.save(bookCommentRely);
        if (idSave) {
            return ResultUtils.success();
        }
        return ResultUtils.error("评论添加失败");
    }


}
